#include <memory>
#include <utility>
#include "VariableHandler.h"

using namespace std;

namespace CExpression
{
	bool VariableHandler::ParseCore(ParseContext &context)
	{
		bool ret = false;
		shared_ptr<ExpressionToken> token=context.Tokens()[context.CurrentTokenIndex()];
		this->ParseVariable(token->Token, context);
        context.IncreaseFunctionParameterCount();
        ret = true;
        return ret;
	}

	void VariableHandler::ParseVariable(const ExpressionString &variableName,ParseContext &context)
	{
		map< ExpressionString,std::shared_ptr<VariableNode> >::iterator iter=context.Variables()->find(variableName);
		if(iter!=context.Variables()->end())
		{
			context.OperandStack().push(iter->second);
		}
		else
		{
			shared_ptr<VariableNode> ptr(new VariableNode(variableName,ExecuteRunTimeType::None));
			context.OperandStack().push(ptr);
			context.Variables()->insert(make_pair(variableName,ptr));
		}
	}
}